package Algorithm.ReviewCode;

/**
 * 杰成牛
 * @param <E>
 */
public class BSTMySelf<E extends Comparable<E>> {

    private class Node{
        E e;
        Node left,right;

        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }
    }

    private Node root;
    private int size;

    public BSTMySelf() {
        root = null;
        size = 0;
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public void add(E e){
        root = add(root,e);
    }

    private Node add(Node node, E e) {

        if(node == null){
            size ++;
            return new Node(e);
        }

        if(e.compareTo(node.e) < 0){
            node.left = add(node.left,e);
        }else if (e.compareTo(node.e) > 0) {
            node.right = add(node.right,e);
        }
        return node;
    }


    public boolean contains(E e){
        return contains(root,e);

    }

    private boolean contains(Node node, E e) {

        if(node == null){
            return false;
        }

        if(e.compareTo(node.e) == 0 ){
            return true;
        } else if (e.compareTo(node.e) < 0) {
            return contains(node.left,e);
        }else {
            return contains(node.right,e);
        }
    }

    public E mininum(){

        if(size == 0){
            throw new IllegalArgumentException("BST is empty");
        }
        Node ret = mininum(root);
        return ret.e;
    }

    //找到最小值所在的节点
    private Node mininum(Node node) {

        if(node.left == null){
            return node;
        }
        return mininum(node.left);
    }


    public E maxinum(){
        if(size == 0){
            throw new IllegalArgumentException("BST is empty");
        }
        Node ret = maxinum(root);
        return ret.e;
    }

    private Node maxinum(Node node) {

        if(node.right == null){
            return node;
        }
        return maxinum(node.right);
    }

    public E removeMin(){
        E mininum = mininum();
        root = removeMin(root);
        return mininum;
    }

    //没理解记住
    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            size --;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    //删除任意元素
    public void remove(E e){
        root = remove(root,e);
    }

    private Node remove(Node node, E e) {

        if (node == null) {
            return null;
        }

        if (e.compareTo(node.e) < 0) {
            node.left = remove(node.left,e);
            return node;
        } else if (e.compareTo(node.e) > 0) {
            node.right = remove(node.right,e);
            return node;
        }else {

            //左子树为空的情况
            if (node.left == null) {
                Node rightNode = node.right;
                size --;
                node.right = null;
                return rightNode;
            }

            if (node.right == null) {
                Node leftNode = node.left;
                size --;
                node.left = null;
                return leftNode;
            }

            //最最复杂的情况
            Node successor = mininum(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;

            //释放掉这个节点
            node.left = node.right = null;
            return successor;
        }


    }


}
